package com.herench.bs.handler;

import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicAppointmentRecordDao;
import com.herench.bs.dao.IClinicChargeRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.*;

/**
 * cleverClinic
 * com.herench.bs.handler
 *
 * @author wangwenkai
 * @date 2017/11/8/13:10
 */
@Component
@Handler(code = "102113" ,name="保存待缴费数据(加医保)")
public class SaveclinicChargeDataInsuranceHandler extends AbstractMessageHandler {

    @Value("${userList}")
    private String userList;

    @Autowired
    private IClinicAppointmentRecordDao clinicAppointmentRecordDao;
    @Autowired
    private IClinicChargeRecordDao clinicChargeRecordDao;
    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        Assert.notNull(channelCode, "channelCode 不可为空！");
        String recordId = requestMessage.getArgsPropertyStringValue("recordId");
        Assert.notNull(recordId, "recordId 不可为空！");
        String patientId = requestMessage.getArgsPropertyStringValue("patientId");
        Assert.notNull(patientId, "patientId 不可为空！");
        String paySeq = requestMessage.getArgsPropertyStringValue("paySeq");
        Assert.notNull(paySeq, "paySeq 不可为空！");
//        String payType = requestMessage.getArgsPropertyStringValue("payType");
//        Assert.notNull(payType, "payType 不可为空！");
        String cardType = requestMessage.getArgsPropertyStringValue("cardType");
        Assert.notNull(cardType, "cardType 不可为空！");
        String cardNumber = requestMessage.getArgsPropertyStringValue("cardNumber");
        String userName = requestMessage.getArgsPropertyStringValue("userName");
        Assert.notNull(userName, "userName 不可为空！");
//        if (StringUtils.hasLength(userList)){
//            String[] tempArray = userList.split(",");
//            List<String> phoneList = Arrays.asList(tempArray);
//            if (!phoneList.contains(userName)){
//                throw new RuntimeException("服务暂未开发，敬请期待!");
//            }
//        }
        String payType = requestMessage.getArgsPropertyStringValue("payType");
        String insuranceType = requestMessage.getArgsPropertyStringValue("insuranceType");
        String mtType = requestMessage.getArgsPropertyStringValue("mtType");
        String source = requestMessage.getArgsPropertyStringValue("source");
        //新增医保卡号
        String insurNo = requestMessage.getArgsPropertyStringValue("insurNo");
        String syjyxm = requestMessage.getArgsPropertyStringValue("syjyxm");
//        Assert.notNull(source, "source 不可为空！");
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        Map<String, Object> params = new HashMap<>(16);
        params.put("channelCode", channelCode);
        params.put("id", recordId);
        params.put("hospitalCode", hospitalCode);
        Map<String, Object> resDao = clinicAppointmentRecordDao.findOne(params);
        String idCard = (String) resDao.get("idCard");
        String resDate = DataFormatUtils.getSimpleTimeString((Date) resDao.get("appointDate"));
        String transactNo = (String) resDao.get("tranNo");
        String visitNo = (String) resDao.get("visitNo");
        Date dt = new Date();
        String text = DataFormatUtils.getWeekOfDate(dt);
        if (!(text.equals("星期六")||text.equals("星期日"))) {
            Map<String, Object> res = new HashMap<>(16);

//            Map<String, Object> resUnpaidOrders = outpatientClinicAgent.loadUnpaidOrders(channelCode, hospitalCode, idCard, source, userName, "3");
//        if (Dialect.getSuccessfulState().equals(resUnpaidOrders.get("code"))) {
//            res.put("dataList",resUnpaidOrders.get("dataList"));
//            res.put(ResponseMessage.RES_CODE, "-001");
//            res.put(ResponseMessage.MESSAGE, "存在未支付订单");
//            res.put(ResponseMessage.MESSAGE_IN, "存在未支付订单");
//            res.put(ResponseMessage.MESSAGE_OUT, "存在未支付订单");
//        } else {

        Map<String, Object> charge = new HashMap<>(16);
        charge.put("patientId",patientId);
        charge.put("clinicChargeStatus","2");
        charge.put("idCard",idCard);
       charge.put("channelCode",channelCode);
        charge.put("hospitalCode",hospitalCode);
        List<Map<String, Object>> resChargeList = new ArrayList<>();
        resChargeList = clinicChargeRecordDao.find(charge);
        if (resChargeList.size()>0){
            recordId = (String) resChargeList.get(0).get("recordId") ;
            paySeq = (String) resChargeList.get(0).get("paySeq") ;
//            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
//            String patientId = (String) resPatientId.get("patientId");
            Map<String, Object> resCharge = new HashMap<>(16);
            resCharge = outpatientClinicAgent.canceleClinicChargePaymentInsurance(channelCode, hospitalCode, recordId, patientId, paySeq);
        }

            res = outpatientClinicAgent.saveclinicChargeDataInsurance(channelCode, hospitalCode, userName, recordId, idCard, resDate, transactNo, patientId, visitNo, paySeq, cardType, cardNumber, payType, insuranceType, mtType, insurNo, syjyxm);
//        }
            responseMessage.setData(res);
            getPropertiesFromInvokerResultMap(responseMessage, res);
        }else{
            Map<String, Object> res=new HashMap<>();
            res.put(ResponseMessage.RES_CODE, "-1");
            res.put(ResponseMessage.MESSAGE, "此功能六日不开放！");
            responseMessage.setData(res);
            getPropertiesFromInvokerResultMap(responseMessage, res);
        }
        return responseMessage;
    }
}
